<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermissionFn(1,'0')">添加权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="primary" size="mini" @click="addPermissionFn(2,row.id)">添加</el-button>
              <el-button type="success" size="mini" @click="editPerFn(row.id)">编辑</el-button>
              <el-button type="danger" size="mini" @click="delPerFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加权限弹窗 -->
      <perDialog ref="perDialogRef" :permission-list="perList" :click-id="clickId" :is-edit="isEdit" :show-dialog.sync="showPerDialog" @confirmEv="confirmFn" />
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils' // 导入处理树形数据结构的方法
import perDialog from './component/perDialog.vue'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 权限列表
      perList: [], // 权限列表(扁平化结构)
      showPerDialog: false, // 弹窗的显示和隐藏
      type: 0, // 1 为一级权限,2 为二级权限
      pid: '', // 0是一级,点击的id是二级
      isEdit: false, // 编辑标记
      clickId: '' // 当前点击项的id
    }
  },
  created() {
    this.getPermissionListFn()
  },
  methods: {
    // 获取权限列表
    async getPermissionListFn() {
      const res = await getPermissionListAPI()
      this.perList = res.data // 保存扁平化的数据结构
      this.permissionList = transTree(res.data, '0')
    },

    // 添加权限按钮
    addPermissionFn(type, pid) {
      this.isEdit = false
      this.showPerDialog = true
      // 保存数据
      this.type = type
      this.pid = pid
      this.clickId = pid
    },

    // 弹窗确定按钮
    async confirmFn(dataObj) {
      dataObj.type = this.type // 向对象里追加数据
      dataObj.pid = this.pid
      if (this.isEdit) {
        const resp = await updatePermissionAPI(dataObj) // 调用编辑接口
        this.$message.success(resp.message)
      } else {
        const res = await addPermissionAPI(dataObj) // 调用添加接口
        this.$message.success(res.message)
      }
      this.getPermissionListFn() // 重新获取权限列表
    },

    // 编辑权限按钮
    async editPerFn(id) {
      this.isEdit = true // 编辑状态
      this.showPerDialog = true
      const res = await getPermissionDetailAPI(id)
      this.$refs.perDialogRef.formData = res.data // 子组件的数据赋值
    },

    // 删除权限按钮
    async delPerFn(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermissionAPI(id)
        this.getPermissionListFn()
        this.$message.success(res.message)
      }).catch(() => {
        this.$message('已取消')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
